package com.stable.web.controller;

import com.stable.service.HorizontalService;
import com.stable.service.NewFinanceService;
import com.stable.service.StockBasicService;
import com.stable.vo.http.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/zwjob")
@RestController
public class ZwJobController {

    @Autowired
    private StockBasicService stockBasicService;
    @Autowired
    private HorizontalService horizontalService;
    @Autowired
    private NewFinanceService newFinanceService;

    /**
     * 更新股票列表到redis
     *
     * @return
     */
    @RequestMapping(value = "/updateCodeList", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> updateCodeList() {
        JsonResult r = new JsonResult();
        try {
            stockBasicService.jobSynCodeList();
            r.setStatus(JsonResult.OK);
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus(JsonResult.ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(r);
    }

    /**
     * 根据同花顺更新公司亮点及质押等数据信息
     *
     * @return
     */
    @RequestMapping(value = "/updateThsTopic", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> updateThsTopic() {
        JsonResult r = new JsonResult();
        try {
            stockBasicService.jobSynCodeTopic();
            r.setStatus(JsonResult.OK);
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus(JsonResult.ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(r);
    }

    /**
     * 更新财务年报
     *
     * @return
     */
    @RequestMapping(value = "/updateFinance", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> updateFinance() {
        JsonResult r = new JsonResult();
        try {
            newFinanceService.jobSynStockNewFinance();
            r.setStatus(JsonResult.OK);
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus(JsonResult.ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(r);
    }

    /**
     * 手动同步股票列表，包含250日线数据；每个参数都非必传
     * 1，如果都不传参数，会同步所有股票的代码，根据近2年数据计算后，只会存包含250日线的日数据
     * 2. 只传tsCode，会更新指定tsCode根据近2年数据计算后，只会存包含250日线的日数据
     * 3. 传tsCode，skipTo=true时，则根据全部股票列表，跳过前面的所有股票，从指定tsCode开始更新，只会存包含250日线的日数据
     * 4. 传tsCode，skipTo=false，startDate=yyyymmdd后，根据tsCode和近2年数据只会存包含250日线的日数据
     * 5. 传tsCode，skipTo=true，startDate=yyyymmdd后，根据tsCode和指定日期数据只会存包含250日线的日数据
     *
     * @param tsCode      代码
     * @param skipTo      必须配合tsCode参数使用，跳至目的tsCode开始更新
     * @param startDate   yyyyMMdd
     * @param insertCount 只插入ES多少条记录
     * @return
     */
    @RequestMapping(value = "/stocklisthistory", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> stocklist(String tsCode,
                                                boolean skipTo,
                                                String startDate,
                                                @RequestParam(name = "insertCount", defaultValue = "300", required = false) int insertCount) {
        JsonResult r = new JsonResult();
        try {
            stockBasicService.jobSynStockProBar(tsCode, skipTo, startDate, insertCount);
            r.setStatus(JsonResult.OK);
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus(JsonResult.ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(r);
    }

}
